package utils;

import service.UserService;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.Statement;

public class DatabaseInitializer {
    
    public static void main(String[] args) {
        UserService userService = new UserService();
        try {
            // 读取 SQL 文件
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                    DatabaseInitializer.class.getResourceAsStream("/sql/init.sql"), 
                    "UTF-8"
                )
            );
            
            StringBuilder sql = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                // 跳过注释行和空行
                if (!line.trim().isEmpty() && !line.trim().startsWith("--")) {
                    sql.append(line).append("\n");
                }
            }
            
            // 执行 SQL 语句
            try (Connection conn = DatabaseUtil.getConnection()) {
                // 分割 SQL 语句并逐个执行
                String[] statements = sql.toString().split(";");
                
                // 先执行创建表的语句
                for (String statement : statements) {
                    if (!statement.trim().isEmpty() && 
                        (statement.trim().toUpperCase().startsWith("CREATE DATABASE") ||
                         statement.trim().toUpperCase().startsWith("USE") ||
                         statement.trim().toUpperCase().startsWith("CREATE TABLE"))) {
                        try (Statement stmt = conn.createStatement()) {
                            System.out.println("正在执行: " + statement.trim());
                            stmt.execute(statement.trim());
                            System.out.println("执行成功!");
                        } catch (Exception e) {
                            System.err.println("执行失败: " + statement.trim());
                            System.err.println("错误信息: " + e.getMessage());
                            if (!statement.trim().toUpperCase().startsWith("CREATE DATABASE")) {
                                throw e;
                            }
                        }
                    }
                }
                
                // 再执行插入数据和修改表的语句
                for (String statement : statements) {
                    if (!statement.trim().isEmpty() && 
                        !statement.trim().toUpperCase().startsWith("CREATE") &&
                        !statement.trim().toUpperCase().startsWith("USE")) {
                        try (Statement stmt = conn.createStatement()) {
                            System.out.println("正在执行: " + statement.trim());
                            stmt.execute(statement.trim());
                            System.out.println("执行成功!");
                        } catch (Exception e) {
                            System.err.println("执行失败: " + statement.trim());
                            System.err.println("错误信息: " + e.getMessage());
                            throw e;
                        }
                    }
                }

                
        
        // 创建管理员账号
        boolean success = userService.createUser("admin", "admin123", "ADMIN");
        if (success) {
            System.out.println("管理员账号创建成功！");
            System.out.println("用户名: admin");
            System.out.println("密码: admin123");
        } else {
            System.out.println("管理员账号创建失败！");
        }
                
                System.out.println("数据库初始化成功！");
                
            } catch (Exception e) {
                System.err.println("数据库初始化失败：" + e.getMessage());
                e.printStackTrace();
            }
            
        } catch (Exception e) {
            System.err.println("读取 SQL 文件失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
} 